package br.com.fatecpg.minhasfinancas;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import br.com.fatecpg.minhasfinancas.model.CartaoModel;
import br.com.fatecpg.minhasfinancas.model.DespesaModel;
import br.com.fatecpg.minhasfinancas.sqlite.CartaoSqlite;

public class CartaoActivity extends Activity {
	CartaoSqlite db = new CartaoSqlite(this);
	private Spinner spn1;
	private List<String> bandeiras = new ArrayList<String>();
	private String bandeira;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cartao);

		AdicionarOnClicks();
		PreencherBandeiras();

	}

	private void AdicionarOnClicks() {
		Button btnSalvar = (Button) findViewById(R.id.btnSalvarReceita);
		btnSalvar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Salvar(v);
			}
		});

		Button btnVoltar = (Button) findViewById(R.id.btnVoltar);
		btnVoltar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoMenu(v);
			}
		});

	}

	private void PreencherBandeiras() {
		bandeiras.add("Visa");
		bandeiras.add("MasterCard");
		bandeiras.add("American Express");
		bandeiras.add("Sorocred");

		spn1 = (Spinner) findViewById(R.id.spnBandeira);

		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, bandeiras);
		ArrayAdapter<String> spinnerArrayAdapter = arrayAdapter;
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_item);
		spn1.setAdapter(spinnerArrayAdapter);

		spn1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v,
					int posicao, long id) {
				bandeira = parent.getItemAtPosition(posicao).toString();

				Toast.makeText(CartaoActivity.this,
						"Nome Selecionado: " + bandeira, Toast.LENGTH_LONG)
						.show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

	}

	public void gotoMenu(View v) {
		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cartao, menu);
		return true;
	}

	private void Salvar(View v) {

		String tipo = null;

		EditText descricao = (EditText) findViewById(R.id.txtDescricaoCartao);
		Spinner bandeira = (Spinner) findViewById(R.id.spnBandeira);
		EditText dataVencimento = (EditText) findViewById(R.id.txtVencimentoCartao);
		RadioButton credito = (RadioButton) findViewById(R.id.rdbCredito);
		RadioButton debito = (RadioButton) findViewById(R.id.rdbDebito);

		if (credito.isChecked())
			tipo = "credito";
		else
			tipo = "debito";

		CartaoModel model = new CartaoModel();

		model.setDescricao(descricao.getText().toString());
		model.setTipo(tipo);
		model.setDiaVencimento(Integer.parseInt(dataVencimento.getText()
				.toString()));
		model.setBandeira(bandeira.getSelectedItem().toString());

		db.Insert(model);

		CarregarLista();

		// db.getAllBooks();

	}

	private void CarregarLista() {

		ListView lista = (ListView) findViewById(R.id.listView1);
		ArrayAdapter<CartaoModel> adapter = new ArrayAdapter<CartaoModel>(this,
				android.R.layout.simple_list_item_1, db.getListaCartao());

		lista.setAdapter(adapter);

	}

}
